package ccb.ygqd.inspur.rdcard;


import android.content.Context;
import android.os.Bundle;

import com.siecomzg.bean.BankCardInfoBean;
import com.siecomzg.constconfig.ConstDefine;
import com.siecomzg.constconfig.ErrorDefine;
import com.siecomzg.tool.LogUtil;
import com.siecomzg.transport.DeviceCallback;
import com.siecomzg.transport.SiecomDevice;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import ccb.ygqd.inspur.common.ConstHF;

/**
 * 磁条读卡器
 */
public class Rdcard  {

    public static final String TAG = Rdcard.class.getSimpleName();

    private Context mContext;
    public Rdcard(Context context){
        mContext = context;
    }

    /**
     *
     * @param iTrackNum 1:一磁道
     * 2:二磁道
     * 3:三磁道
     * 输入参数可以是1/2/3的组合，比如23表示读二、三磁道，123表示读一、二、三磁道。
     * @param strTimeout 默认20秒；
     *
     * @return 如果aryRet[0]为DRV0000000。aryRet[1]存放一磁道数据, aryRet[2]存放二磁道数据，aryRet[3]存放三磁道数据，
     * 磁条数据均明文返回，如某磁道没读到，则String数组的该磁道内容返回空字符串
     */
    public String[] getCardNum(final int iTrackNum, String strTimeout){
        final String ret[] = new String[]{"","","",""};
        if (!SiecomDevice.isConnected){
            ret[0] = ConstHF.ERR_NOT_CONN;
            ret[1] = ConstHF.ERR_DEV_ONT_CONN;
            return ret;
        }
        int timeOut = Integer.parseInt(strTimeout);
        final CountDownLatch latch = new CountDownLatch(1);
        SiecomDevice.readBankCard(ConstDefine.MSR_CARD, new DeviceCallback() {
            @Override
            public void onStart() {

            }

            @Override
            public void onError(Bundle bundle) {
                int code = bundle.getInt("code");
                String msg = bundle.getString("message");
                LogUtil.e(TAG,"code == " + code + " ---- " + msg);
                ret[0] = ConstHF.ERR_MSR_READ_FAIL; // 读取错误
                ret[1] = msg;
                if (code == ErrorDefine.MSR_CARD_TIME){
                    ret[0] = ConstHF.ERR_TIME_OUT;
                    ret[1] = ErrorDefine.getErrorDescribe(ErrorDefine.MSR_CARD_TIME);
                }
                if (code == ErrorDefine.ID_CANCEL_READ){
                    ret[0] = ConstHF.ERR_USER_CANCEL;
                    ret[1] = "取消刷卡";
                }
                latch.countDown();
            }

            @Override
            public void onSuccess(Bundle bundle) {
                ret[0] = ConstHF.ERR_SUCCESS;
                BankCardInfoBean bean = bundle.getParcelable("data");
                String trackNum = iTrackNum + "";
                if (trackNum.contains("1")){
                    ret[1] = bean.oneMagneticTrack;
                }
                if (trackNum.contains("2")){
                    ret[2] = bean.twoMagneticTrack;
                }
                if (trackNum.contains("3")){
                    ret[3] = bean.threeMagneticTrack;
                }
                latch.countDown();
            }

            @Override
            public void onCompleted(Bundle bundle) {
                latch.countDown();
            }
        },null,null,timeOut*1000,false);
        try {
            latch.await(timeOut + 2, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        return ret;
    }
}
